<%--
  Created by IntelliJ IDEA.
  User: yyf19
  Date: 2019/6/14
  Time: 17:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>订单提交</title>
    <%@ include file="/WEB-INF/jsp/include/head.jsp" %>
</head>
<body>
<div>
    <%@ include file="/WEB-INF/jsp/include/nav.jsp" %>
</div>
<div class="container" id="product-box">

    <%--<hr/>--%>
    <div class="row" id="shipping-list">

    </div>

    <table class="table table-bordered" id="myTable">
        <tr>
            <th></th>
            <th>名称</th>
            <th>价格</th>
            <th>数量</th>
            <%--<th>状态</th>--%>
            <%--<th>是否选中</th>--%>
        </tr>
    </table>
        <div style="clear: both">
            <i>运费：￥</i><b id="postage"></b>
        </div>
    <div style="clear: both">
        <h2 style="float: left">总价:</h2>
        <h2 id="total-price" style="color: red;float: left;"></h2>
    </div>
    <div style="float: right">
        <button type="button" class="btn btn-default" onclick="${serverApp}/user/${SESSION_USER.id}/cart/toCart">取消</button>
        <a type="button" class="btn btn-warning"  onclick="createOrder()">提交订单</a>
    </div>

</div>

</body>
<%@ include file="/WEB-INF/jsp/include/bodyfoot.jsp" %>
<script>
    var shippingId;
    var url = serverApp + "/user/${SESSION_USER.id}/cart/listDetailChecked";
    var trs = ""
    var productIds = []
    var total = 0;
    var postage = 0;
    var product;
    $GET(url, function (result) {
        product = result;
        for (var i = 0; i < result.length; i++) {
            productIds.push(result[i].productId)

            trs += '<tr>'
            trs += '<td><a href="' + serverApp + '/product/toGet/' + result[i].productId + '" class="thumbnail main-image-box-80-80" ><img src="' + result[i].mainImage + '" alt="..."> </a></td>'
            trs += '<td>' + result[i].name + '</td>'
            trs += '<td>' + result[i].price + '</td>'
            trs += '<td>' + result[i].quantity + '</td>'
            // trs += '<td>' + result[i].status + '</td>'
            // trs+='<td>'+result[i].checked+'</td>'
            trs += '</tr>'
        }

        var $myTable = $("#myTable");
        $myTable.append(trs)

    })

    function clearAll() {
        var url = serverApp + "/user/${SESSION_USER.id}/cart/deleteAll";
        $GET(url, function (result) {
            console.log(result)
            window.location.href = window.location.href
        })
    }

    var shippingUrl = serverApp + "/user/${SESSION_USER.id}/shipping/list";
    $GET(shippingUrl, function (result) {
        var divs = "";
        for(var i=0;i<result.length;i++){
            divs+='<div class="col-md-2 shpping-box" onclick="checkShipping(this)" data-id="'+result[i].id+'"><p>'+result[i].receiverName+'</p><p>'+result[i].receiverPhone+'</p>' +
                '<p>'+result[i].receiverProvince+result[i].receiverCity+result[i].receiverDistrict+result[i].receiverAddress+'</p><p>'+result[i].receiverZip+'</p></div>'
        }
        $("#shipping-list").html(divs);
        $("#shipping-list").find("div").eq(0).click();
    })

    function checkShipping(mythis) {
        total = 0;
        var url = serverApp + "/user/${SESSION_USER.id}/shipping/getPostage";
        $this = $(mythis)
        shippingId = $this.attr('data-id')
        $this.parent().children().each(function(index,e){
            $(e).css("border","1px solid gray")
        })
        $this.css("border","1px solid red")
        $POST_JSON(url,{shippingId:shippingId,productIds:productIds},function (res) {
            postage = res;
            $("#postage").html(res)
            calTotal();
        })
    }

    function calTotal() {
        total=0;
        for(var i=0;i<product.length;i++){
            if(product[i].checked == 1){
                total += product[i].price * product[i].quantity
            }
        }
        total += postage;
        $("#total-price").html('￥' + total);
    }
    
    function createOrder() {
        var url = serverApp + "/user/${SESSION_USER.id}/order/create";
        var data={
            shippingId:shippingId
        }
        $POST_JSON(url,data,function (e) {

        })
    }


</script>
</html>
